<?php

class ReportController extends BaseController
{
	public $leftmenusTitle = '常用操作';
	
	
	protected function beforeAction($action)
    {
    	parent::beforeAction();
    	$this->leftmenus = array();    	
    		# code...
    	$this->leftmenus[] = array('href'=>$this->createUrl('report/treaty'),'menutext'=>'合约销售日报','isValid'=>Yii::app()->user->checkAccess('MENU2_HYXSRB'));
    	$this->leftmenus[] = array('href'=>$this->createUrl('report/general'),'menutext'=>'裸机销售日报','isValid'=>Yii::app()->user->checkAccess('MENU2_LJXSRB'));
    	$this->leftmenus[] = array('href'=>$this->createUrl('report/lend'),'menutext'=>'产品外借日报','isValid'=>Yii::app()->user->checkAccess('MENU2_CPWJRB'));
    	$this->leftmenus[] = array('href'=>$this->createUrl('report/profit'),'menutext'=>'毛利日报','isValid'=>Yii::app()->user->checkAccess('MENU2_MLRB'));
    	$this->leftmenus[] = array('href'=>$this->createUrl('report/purchase'),'menutext'=>'商品请购单','isValid'=>Yii::app()->user->checkAccess('MENU2_SPQGD'));
    	Yii::app()->clientScript->registerCoreScript('jquery');
        return true;
    } 


	/**
	 * 合约销售日报
	 */
	public function actionTreaty()
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/depart'));
		$this->title='合约销售日报';
		$model=new SearchForm;
		$criteria=new CDbCriteria(array(  
			   //'defaultorder'=>'trade_date',  
			   'with'=>array('discntCode','inchannelCode','producttypeCode','product','tradeStaff','tradeMethod','tradeUserType'),
			)); 

		if(isset($_GET['SearchForm']))
		{
			$model->attributes=$_GET['SearchForm'];
		}
		
		else
		{
			$model->attributes=array(
				'eparchy_code'=>Yii::app()->session['currenUser']->eparchy_code,
			    'depart_code'=>Yii::app()->session['currenUser']->depart_code,
			    'start_date'=>date('Y-m-d'),
			    'end_date'=>date('Y-m-d'),
			);
		}
		
		if(!isset($_GET['export']))
		{
			$model->saveCondition();
		}
		
		if($model->validate())
		{
			$criteria->addSearchCondition('tradeStaff.eparchy_code',isset($_POST['export'])?$model->old_eparchy_code:$model->eparchy_code);
			$criteria->addSearchCondition('tradeStaff.depart_code',isset($_POST['export'])?$model->old_depart_code:$model->depart_code);
			$criteria->compare('trade_date','>='.(isset($_POST['export'])?$model->old_start_date:$model->start_date));
			$criteria->compare('trade_date','<='.(isset($_POST['export'])?$model->old_end_date:$model->end_date));
		}
		
		$dataProvider=new CActiveDataProvider('TradeTreaty', array(
			    'criteria'=>$criteria,
			    'pagination'=>array(
			        'pageSize'=>Yii::app()->params['reportsPerPage'],
			    ),
			));
		if(isset($_GET['export']))
		{
			$this->renderPartial('treatyexport',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
		else
		{
			//Yii::log(CJSON::encode($criteria),'info');
			$this->render('treaty',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
		
	}
	
	public function Actiontreatydelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			TradeTreaty::model()->findByPk($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
	/**
	 * 裸机销售日报
	 */
	public function actionGeneral()
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/depart'));
		$this->title='裸机销售日报';
		$model=new SearchForm;
		$criteria=new CDbCriteria(array(  
			   //'defaultorder'=>'trade_date',  
			   'with'=>array('product','producttypeCode','tradeMethod','tradeStaff'),
			)); 

		if(isset($_GET['SearchForm']))
		{
			$model->attributes=$_GET['SearchForm'];
		}
		
		else
		{
			$model->attributes=array(
			    'depart_code'=>Yii::app()->session['currenUser']->depart_code,
			    'start_date'=>date('Y-m-d'),
			    'end_date'=>date('Y-m-d'),
			);
		}
		
		if(!isset($_GET['export']))
		{
			$model->saveCondition();
		}
		
		if($model->validate())
		{
			$criteria->addSearchCondition('tradeStaff.eparchy_code',isset($_POST['export'])?$model->old_eparchy_code:$model->eparchy_code);
			$criteria->addSearchCondition('tradeStaff.depart_code',isset($_GET['export'])?$model->old_depart_code:$model->depart_code);
			$criteria->compare('trade_date','>='.(isset($_GET['export'])?$model->old_start_date:$model->start_date));
			$criteria->compare('trade_date','<='.(isset($_GET['export'])?$model->old_end_date:$model->end_date));
		}
		
		$dataProvider=new CActiveDataProvider('TradeGeneral', array(
			    'criteria'=>$criteria,
			    'pagination'=>array(
			        'pageSize'=>Yii::app()->params['reportsPerPage'],
			    ),
			));
		if(isset($_GET['export']))
		{
			$this->renderPartial('generalexport',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
		else
		{
			//Yii::log(CJSON::encode($criteria),'info');
			$this->render('general',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
	}
	
	public function ActionGeneraldelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			TradeGeneral::model()->findByPk($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * 产品外借日报
	 */
	public function actionLend()
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/depart'));
		$this->title='产品外借日报';
		$model=new SearchForm;
		$criteria=new CDbCriteria(array(  
			   //'defaultorder'=>'trade_date',  
			   'with'=>array('product','lendStaff','outMethod','producttypeCode'),
			)); 

		if(isset($_GET['SearchForm']))
		{
			$model->attributes=$_GET['SearchForm'];
		}
		
		else
		{
			$model->attributes=array(
			    'depart_code'=>Yii::app()->session['currenUser']->depart_code,
			    'start_date'=>date('Y-m-d'),
			    'end_date'=>date('Y-m-d'),
			);
		}
		
		if(!isset($_GET['export']))
		{
			$model->saveCondition();
		}
		
		if($model->validate())
		{
			$criteria->addSearchCondition('lendStaff.eparchy_code',isset($_POST['export'])?$model->old_eparchy_code:$model->eparchy_code);
			$criteria->addSearchCondition('lendStaff.depart_code',isset($_GET['export'])?$model->old_depart_code:$model->depart_code);
			$criteria->compare('lend_date','>='.(isset($_GET['export'])?$model->old_start_date:$model->start_date));
			$criteria->compare('lend_date','<='.(isset($_GET['export'])?$model->old_end_date:$model->end_date));
		}
		
		$dataProvider=new CActiveDataProvider('Lend', array(
			    'criteria'=>$criteria,
			    'pagination'=>array(
			        'pageSize'=>Yii::app()->params['reportsPerPage'],
			    ),
			));
		if(isset($_GET['export']))
		{
			$this->renderPartial('lendexport',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
		else
		{
			//Yii::log(CJSON::encode($criteria),'info');
			$this->render('lend',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
	}
	public function ActionLenddelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			Lend::model()->findByPk($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
	/**
	 * 毛利日报
	 */
	public function actionProfit()
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/depart'));
		$this->title='毛利日报';
		$model=new SearchForm;
		$criteria=new CDbCriteria(array(  
			   //'defaultorder'=>'trade_date',  
			   'with'=>array('product','producttypeCode','tradeType','tradeStaff'),
			)); 

		if(isset($_GET['SearchForm']))
		{
			$model->attributes=$_GET['SearchForm'];
		}
		
		else
		{
			$model->attributes=array(
			    'depart_code'=>Yii::app()->session['currenUser']->depart_code,
			    'start_date'=>date('Y-m-d'),
			    'end_date'=>date('Y-m-d'),
			);
		}
		
		if(!isset($_GET['export']))
		{
			$model->saveCondition();
		}
		
		if($model->validate())
		{
			$criteria->addSearchCondition('tradeStaff.eparchy_code',isset($_POST['export'])?$model->old_eparchy_code:$model->eparchy_code);
			$criteria->addSearchCondition('tradeStaff.depart_code',isset($_GET['export'])?$model->old_depart_code:$model->depart_code);
			$criteria->compare('trade_date','>='.(isset($_GET['export'])?$model->old_start_date:$model->start_date));
			$criteria->compare('trade_date','<='.(isset($_GET['export'])?$model->old_end_date:$model->end_date));
		}
		
		$dataProvider=new CActiveDataProvider('Profit', array(
			    'criteria'=>$criteria,
			    'pagination'=>array(
			        'pageSize'=>Yii::app()->params['reportsPerPage'],
			    ),
			));
		if(isset($_GET['export']))
		{
			$this->renderPartial('profitexport',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
		else
		{
			//Yii::log(CJSON::encode($criteria),'info');
			$this->render('profit',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
	}
	
	/**
	 * 商品请购日报
	 */
	public function actionPurchase()
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/depart'));
		$this->title='商品请购单';
		$model=new SearchForm;
		$criteria=new CDbCriteria(array(  
			   //'defaultorder'=>'trade_date',  
			   'with'=>array('product','departCode'),
			)); 

		if(isset($_GET['SearchForm']))
		{
			$model->attributes=$_GET['SearchForm'];
		}
		
		else
		{
			$model->attributes=array(
			    'depart_code'=>Yii::app()->session['currenUser']->depart_code,
			    'start_date'=>date('Y-m-d'),
			    'end_date'=>date('Y-m-d'),
			);
		}
		
		if(!isset($_GET['export']))
		{
			$model->saveCondition();
		}
		
		if($model->validate())
		{
			$criteria->addSearchCondition('departCode.eparchy_code',isset($_POST['export'])?$model->old_eparchy_code:$model->eparchy_code);
			$criteria->addSearchCondition('t.depart_code',isset($_GET['export'])?$model->old_depart_code:$model->depart_code);
			$criteria->compare('purchase_date','>='.(isset($_GET['export'])?$model->old_start_date:$model->start_date));
			$criteria->compare('purchase_date','<='.(isset($_GET['export'])?$model->old_end_date:$model->end_date));
		}
		
		$dataProvider=new CActiveDataProvider('Purchase', array(
			    'criteria'=>$criteria,
			    'pagination'=>array(
			        'pageSize'=>Yii::app()->params['reportsPerPage'],
			    ),
			));
		if(isset($_GET['export']))
		{
			$this->renderPartial('purchaseexport',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
		else
		{
			//Yii::log(CJSON::encode($criteria),'info');
			$this->render('purchase',array('model'=>$model,'dataProvider'=>$dataProvider));
		}
	}
	
	public function ActionPurchasedelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			Purchase::model()->findByPk($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}


}